<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:f="livcos.org/ns/xslt/function"
	xmlns:doc="livcos.org/ns/doc"
	xmlns:pm="livcos.org/ns/pm"
	xmlns="http://www.w3.org/1999/XSL/Format"
	exclude-result-prefixes="doc pm f xs">
	
	<xsl:import href="../../pm/ApplicationsTimeline.xsl"/>
	
	<xsl:param name="ref" select="'../../data/pm/Roadmap'"/>

	<xsl:variable name="dayWidth" select="0.1"/>
  
  <xsl:attribute-set name="border">
    <xsl:attribute name="border-width">thin</xsl:attribute>
    <xsl:attribute name="border-color">#AAAAAA</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="border-top" use-attribute-sets="border">
    <xsl:attribute name="border-top-style">dashed</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="border-left" use-attribute-sets="border">
    <xsl:attribute name="border-left-style">dashed</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:template match="/*">
    <xsl:variable name="numCols" select="count($months)"/>
    <table table-layout="fixed" width="99%" margin-left="0.6cm" font-size="6pt">
      <table-column column-width="18%"/>
      <table-column column-width="81%"/>
      <table-header font-weight="normal">
        <table-row>
          <table-cell padding="0pt">
            <block>&#160;</block>
            <block>&#160;</block>
          </table-cell>
          <table-cell padding="0pt" margin="0pt">
            <xsl:for-each select="$months">
              <xsl:variable name="d" select="days-from-duration(. - $first)"/>
              <block-container left="{$d * $dayWidth}mm" absolute-position="absolute">
                <xsl:choose>
                  <xsl:when test="(position() = 1) or (month-from-date(.) = 1)">
                    <block xsl:use-attribute-sets="border-left" padding-left="1pt">
                      <xsl:value-of select="format-number(year-from-date(.), '0000')"/>
                    </block>
                  </xsl:when>
                  <xsl:otherwise>
                    <block>&#160;</block>
                  </xsl:otherwise>
                </xsl:choose>
              	<block xsl:use-attribute-sets="border-left" padding-left="1pt">
              		<inline font-size="5pt"><xsl:value-of select="format-number(month-from-date(.), '00')"/></inline>
                </block>
              </block-container>
            </xsl:for-each>
          </table-cell>
        </table-row>
      </table-header>
      <table-body>
        <xsl:apply-templates select="$apps" />
        <table-row>
          <table-cell margin="0pt" padding="2pt" xsl:use-attribute-sets="border-top">
            <block>&#160;</block>
          </table-cell>
          <table-cell margin="0pt" padding="2pt" xsl:use-attribute-sets="border-top">
            <block>&#160;</block>
          </table-cell>
        </table-row>
      </table-body>
    </table>
  </xsl:template>
  
  <xsl:template match="pm:application">
    <table-row>
      <table-cell margin="0pt" padding="2pt" xsl:use-attribute-sets="border-top">
        <block margin-left="0pt" padding="0pt">
          <xsl:value-of select="doc:title" />
        </block>
      </table-cell>
      <table-cell margin="0pt" xsl:use-attribute-sets="border-top" padding="2pt">
        <block margin-left="0pt" text-align="center">
        </block>
      </table-cell>
    </table-row>
    <xsl:apply-templates select="pm:release" />
  </xsl:template>
  
  <xsl:template match="pm:release">
    <table-row>
      <table-cell margin="0pt" xsl:use-attribute-sets="border-top" padding="2pt">
        <block margin-left="4pt">
          <xsl:value-of select="@num"/>
          <xsl:if test="@label">
            <xsl:text> </xsl:text>
            <xsl:value-of select="@label"/>
          </xsl:if>
        </block>
      </table-cell>
      <table-cell margin="0pt" xsl:use-attribute-sets="border-top" padding="0pt">
        <xsl:variable name="s" select="days-from-duration(xs:date(@startDate) - $first)"/>
				<xsl:variable name="d"
					select="days-from-duration(xs:date(@releaseDate) - xs:date(@startDate))"/>
        <xsl:choose>
        	<xsl:when test="not(@startDate and @releaseDate)">
						<block text-align="right" padding-right="1pt" font-size="6pt" color="white"
							>&#160;</block>
        	</xsl:when>
        	<xsl:when test="$d &gt; 100">
						<block-container left="{$s * $dayWidth}mm" top="3pt" absolute-position="absolute"
							width="{$d * $dayWidth}mm" background-color="#6666BB">
							<block text-align="right" padding-top="1pt" padding-right="1pt" font-size="4pt"
								color="white">
								<xsl:value-of select="@releaseDate"/>
							</block>
						</block-container>
        	</xsl:when>
        	<xsl:otherwise>
        		<block-container left="{$s * $dayWidth}mm" top="3pt" absolute-position="absolute"
        			width="{$d * $dayWidth}mm" background-color="#6666BB">
							<block font-size="4pt">&#160;</block>
        		</block-container>
						<block-container left="{($s + $d) * $dayWidth + 0.1}mm" top="3.5pt" absolute-position="absolute">
							<block text-align="left" font-size="4pt">
								<xsl:value-of select="@releaseDate"/>
							</block>
						</block-container>
        	</xsl:otherwise>
        </xsl:choose>
      </table-cell>
    </table-row>
  </xsl:template>
  
</xsl:stylesheet>
